/*

Generate electricty shares IV

SEDS variables: CLEIB, NGEIB, PAEIB, WWEIB, NUEGB, GEEGB, HYEGB, TEEIB, CLEIV, 
	NGEIV, PAEIV, WWEIV, NUEGV, PEEIV, CLEID, NGEID, PAEID, WWEID, NUEGD

Last revision: 5/11/2021 by Katherine Wagner

*/

clear all

gl dataRAW  "C:\Users\KATHERENE\Dropbox\work_in_progress\carbon_lockin\dataRAW\"
gl dataSTATA  "C:\Users\KATHERENE\Dropbox\work_in_progress\carbon_lockin\dataSTATA\"

* Read in and append all relevant SEDS csv files

cd "${dataRAW}user_provided_data\seds\EIA_SED_Data-20200519T151438Z-001\EIA_SED_Data\seds_input\"
cap erase "${dataSTATA}seds\seds_rawformatted.dta"

* loop over csv files only 
local files : dir . files "*.csv"
local i =  0

foreach f of local files {

	drop _all
	insheet using "`f'"
	
	gen series = substr("`f'",6,5)
	gen state  = substr("`f'",12,2)
	
	if `i' > 0 append using "${dataSTATA}seds\seds_rawformatted.dta"
	sleep 4000
	save "${dataSTATA}seds\seds_rawformatted.dta", replace
	local i = 1
	
}

*>-- Calculate shares in total energy consumed TEEIB (in billion Btu) and in total energy expenditures PEEIV (in million $) --<

drop series_id units

reshape wide value, i(year state) j(series) string
renpfix value

la var teeib "Total Energy Consumed in the Electric Power Sector (Billion Btu)" 
la var peeiv "Total Energy Expenditures in the Electric Power Sector (Million $)"

foreach var in cl ng pa ww{
	gen elecShareB_`var' = `var'eib/teeib
	replace elecShareB_`var' = 0 if mi(elecShareB_`var') & !mi(`var'eib) & !mi(teeib)	
	gen elecShareV_`var' = `var'eiv/peeiv
	replace elecShareV_`var' = 0 if mi(elecShareV_`var') & !mi(`var'eiv) & !mi(peeiv)
	la var `var'eiv "`var': Energy Expenditures in the Electric Power Sector (Million $)"
	la var `var'eib "`var': Energy Consumed in the Electric Power Sector (Billion Btu)"
	la var `var'eid "`var': Price of Energy in the Electric Power Sector ($ per Billion Btu)"
	foreach var2 in B V {
		replace elecShare`var2'_`var'=0 if elecShare`var2'_`var'<0	
	}
}

foreach var in nu{
	gen elecShareB_`var' = `var'egb/teeib
	replace elecShareB_`var' = 0 if mi(elecShareB_`var') & !mi(`var'egb) & !mi(teeib)
	gen elecShareV_`var' = `var'egv/peeiv
	replace elecShareV_`var' = 0 if mi(elecShareV_`var') & !mi(`var'egv) & !mi(peeiv)
	la var `var'egv "`var': Energy Expenditures in the Electric Power Sector (Million $)"
	la var `var'egb "`var': Energy Consumed in the Electric Power Sector (Billion Btu)"
	la var `var'egd "`var': Price of Energy in the Electric Power Sector ($ per Billion Btu)"
	foreach var2 in B V{
		replace elecShare`var2'_`var'=0 if elecShare`var2'_`var'<0
	}
}
	
foreach var in ge hy{
	gen elecShareB_`var' = `var'egb/teeib
	replace elecShareB_`var' = 0 if mi(elecShareB_`var') & !mi(`var'egb) & !mi(teeib)
	la var `var'egb "`var': Energy Consumed in the Electric Power Sector (Billion Btu)"
	foreach var2 in B{
		replace elecShare`var2'_`var' = 0 if elecShare`var2'_`var'<0
	}
}

tempfile seds_formatted
sa `seds_formatted', replace
save "${dataSTATA}seds\seds_rawformatted.dta", replace

*>---- Calculate national leave-out mean consumption-weighted energy prices ---<

drop if state == "us"
tempfile temp
save `temp', replace
	
keep state year
rename state statecode
joinby year using `temp'
drop if state == statecode
foreach var of varlist cl* ng* pa* ww* nu*{
	quietly replace `var' = 0 if `var' == .
}

foreach var in cl ng pa ww {
	bysort statecode year: egen consumptionTotal = sum(`var'eiv)
	gen share = `var'eiv/consumptionTotal
	gen efPriceNational`var' = `var'eid*share
	drop share consumptionTotal
}

foreach var in nu{
	bysort statecode year: egen consumptionTotal = sum(`var'egv)
	gen share = `var'egv/consumptionTotal
	gen efPriceNational`var' = `var'egd*share
	drop share consumptionTotal
}

collapse (sum) efP*, by(statecode year) fast
rename statecode state
foreach var in cl ng pa ww nu{
	label var efPriceNational`var' "`var': National Price for Elec. Gen. (Leave out, Consumption Weighted Mean)"
}

merge 1:1 state year using `seds_formatted', assert(2 3) keep(3) nogen // dropping US

encode state, gen(state_id)
tsset state_id year
	
*>---------------------------- Generate lagged shares -------------------------<

foreach var in 0 2 5{
	foreach var2 in cl ng pa ww nu{
		gen L`var'elecShareB_`var2' = L`var'.elecShareB_`var2'
		la var L`var'elecShareB_`var2' "Lag `var' of `var2' Share in Elec. Power Sector (Btu)" 
		gen L`var'elecShareV_`var2' = L`var'.elecShareV_`var2'
		la var L`var'elecShareV_`var2' "Lag `var' of `var2' Share in Elec. Power Sector (Expenditure)"
	}
}

foreach var in 0 2 5{
	foreach var2 in ge hy{
		gen L`var'elecShareB_`var2' = L`var'.elecShareB_`var2'
		la var L`var'elecShareB_`var2' "Lag `var' of `var2' Share in Elec. Power Sector (Btu)"	
	}
}

*>------ Predict electricity prices based on fuel shares and fuel prices ------<

* build IV = energy price x (lagged) shares


drop state_id
replace state = upper(state)
save "${dataSTATA}seds\seds_iv.dta", replace
export delimited using "${dataSTATA}seds\seds_iv", replace

*>--------------- Export 1976 shares for maps ---------------------------------<
preserve
keep if year == 1976
keep L0elecShareB_cl L0elecShareB_ng L0elecShareB_pa state
ren state abbr
export delimited using "C:\Users\KATHERENE\Dropbox\work_in_progress\carbon_lockin\dataSTATA\seds\shares1976.csv", replace
restore

preserve
keep if year == 2011
keep L0elecShareB_cl L0elecShareB_ng L0elecShareB_pa state
ren state abbr
export delimited using "C:\Users\KATHERENE\Dropbox\work_in_progress\carbon_lockin\dataSTATA\seds\shares2011.csv", replace
restore

preserve
keep if year == 1976 | year == 2011
keep L0elecShareB_cl L0elecShareB_ng L0elecShareB_pa state year
ren state abbr
reshape wide L0elecShareB_cl L0elecShareB_ng L0elecShareB_pa, i(abbr) j(year)
gen delta_coal = L0elecShareB_cl2011 - L0elecShareB_cl1976
gen delta_ng   = L0elecShareB_ng2011 - L0elecShareB_ng1976
gen delta_pa   = L0elecShareB_pa2011 - L0elecShareB_pa1976
keep delta* abbr
export delimited using "C:\Users\KATHERENE\Dropbox\work_in_progress\carbon_lockin\dataSTATA\seds\changeinshares.csv", replace
restore


/*
codes:

CLEIB	Coal consumed by the electric power sector.	Billion Btu
CLEID	Coal price in the electric power sector.	Dollars per million Btu
CLEIK	Factor for converting coal consumed by the electric power sector from physical units to Btu.	Million Btu per short ton
CLEIP	Coal consumed by the electric power sector.	Thousand short tons

DFEIB	Distillate fuel oil consumed by the electric power sector.	Billion Btu
DFEID	Distillate fuel oil price in the electric power sector.	Dollars per million Btu
DFEIP	Distillate fuel oil consumed by the electric power sector.	Thousand barrels
DFEIV	Distillate fuel oil expenditures in the electric power sector.	Million dollars

DKEIB	Distillate fuel oil and kerosene-type jet fuel consumed by the electric power sector.	Billion Btu
DKEID	Distillate fuel oil and kerosene-type jet fuel average price in the electric power sector.	Dollars per million Btu
DKEIP	Distillate fuel oil and kerosene-type jet fuel consumed by the electric power sector.	Thousand barrels
DKEIV	Distillate fuel oil and kerosene-type jet fuel expenditures in the electric power sector.	Million dollars

GEEGB	Geothermal energy consumed for electricity generation by the electric power sector.	Billion Btu

HYEGB	Hydropower consumed for electricity generation by the electric power sector.	Billion Btu

NGEIB	Natural gas consumed by the electric power sector (including supplemental gaseous fuels).	Billion Btu
NGEID	Natural gas price in the electric power sector (including supplemental gaseous fuels).	Dollars per million Btu
NGEIK	Factor for converting natural gas consumed by the electric power sector from physical units to Btu.	Thousand Btu per cubic foot
NGEIP	Natural gas consumed by the electric power sector (including supplemental gaseous fuels).	Million cubic feet
NGEIV	Natural gas expenditures in the electric power sector (including supplemental gaseous fuels).	Million dollars

NNEIB	Natural gas consumed by the electric power sector (excluding supplemental gaseous fuels).  (Code used in SEDS 2006.)	Billion Btu

NUEGB	Nuclear energy consumed for electricity generation by the electric power sector.	Billion Btu
NUEGD	Nuclear fuel price in the electric power sector.	Dollars per million Btu
NUEGP	Nuclear electricity net generation in the electric power sector.	Million kilowatthours
NUEGV	Nuclear fuel expenditures in the electric power sector.	Million dollars
NUETB	Nuclear electric power consumed.	Billion Btu

PAEIB	All petroleum products consumed by the electric power sector.	Billion Btu
PAEID	All petroleum products average price in the electric power sector.	Dollars per million Btu
PAEIK	Factor for converting all petroleum products consumed by the electric power sector from physical units to Btu, United States only.	Million Btu per barrel
PAEIP	All petroleum products consumed by the electric power sector.	Thousand barrels
PAEIV	All petroleum products total expenditures in the electric power sector.	Million dollars

PCEIB	Petroleum coke consumed by the electric power sector.	Billion Btu
PCEID	Petroleum coke price in the electric power sector.	Dollars per million Btu
PCEIP	Petroleum coke consumed by the electric power sector.	Thousand barrels
PCEIV	Petroleum coke expenditures in the electric power sector.	Million dollars

PEEID	Primary energy average price in the electric power sector.	Dollars per million Btu
PEEIV	Primary energy total expenditures in the electric power sector.	Million dollars
PEICD	Primary energy average price in the industrial sector.	Dollars per million Btu
PEICV	Primary energy total expenditures in the industrial sector.	Million dollars

RFEIB	Residual fuel oil consumed by the electric power sector.	Billion Btu
RFEID	Residual fuel oil price in the electric power sector.	Dollars per million Btu
RFEIP	Residual fuel oil consumed by the electric power sector.	Thousand barrels
RFEIV	Residual fuel oil expenditures in the electric power sector.	Million dollars

SFEIB	Supplemental gaseous fuels consumed by the electric power sector.	Billion Btu

SOEGB	Photovoltaic and solar thermal energy consumed for electricity generation by the electric power sector.	Billion Btu

TEEIB	Total energy consumed by the electric power sector.	Billion Btu

WDEIB	Wood consumed by the electric power sector.	Billion Btu
WSEIB	Waste consumed by the electric power sector.	Billion Btu
WWEIB	Wood and waste consumed by the electric power sector.	Billion Btu
WWEID	Wood and waste price in the electric power sector.	Dollars per million Btu

WYEGB	Wind energy consumed for electricity generation by the electric power sector.	Billion Btu







*/
